#课后作业
#给定一个列表, 根据grade来排序

#classes = [
   # {"name": "n_1", "age": 24, "grade": "A"},
    #{"name": "n_2", "age": 23, "grade": "B"},
   # {"name": "n_3", "age": 28, "grade": "A"},
   # {"name": "n_4", "age": 24, "grade": "A"},
   # {"name": "n_5", "age": 25, "grade": "C"},
   # {"name": "n_6", "age": 21, "grade": "D"},
   # {"name": "n_7", "age": 27, "grade": "A"},
#]
#通过filter语句来筛选出Grade为A的同学

#通过map函数将上述同学的age + 1(对原数据结构有影响, 尽量不要用lambda)

#2使用递归函数重构斐波那契函数   f(n) = f(n-1) + f(n-2)

classes = [
    {"name": "n_1", "age": 24, "grade": "A"},
    {"name": "n_2", "age": 23, "grade": "B"},
    {"name": "n_3", "age": 28, "grade": "A"},
    {"name": "n_4", "age": 24, "grade": "A"},
    {"name": "n_5", "age": 25, "grade": "C"},
    {"name": "n_6", "age": 21, "grade": "D"},
    {"name": "n_7", "age": 27, "grade": "A"},
]
# 以grade 排序
classes.sort(key=lambda x:x['grade'])
print(classes)
for item in classes:
    print(item)


#筛选 grade=A的同学
frade_A=filter(lambda x:True if x["grade"]=='A' else False,classes)
a=list(frade_A)
for item in a:
    print(item)


# 为同学年龄+1
def age(item):
    item['age']=item['age']+1
    return item

new_class=map(age,classes)
print(list(new_class))

#[0,1,1,2,3,5,f(n)]

# def f(n):
#     return l[n-1]+l[n-2]
    # if n < 2:
    #     return l

    # f(n) = f(n-1) + f(n-2)
    # return f(n)

    # l.append(f(n))

# [0,1,1,2,3,5,8]


feibo=[]
def feibo_item(n):
    if n==1:
        return 1
    elif  n==0:
        return 0
    return feibo_item(n-1)+feibo_item(n-2)

print(feibo)

def feibo_list(n):
    for i in range(n):
        feibo.append(feibo_item(i))
feibo_list(5)
print(feibo)